package com.doge.service.wx.address;

import com.doge.bean.pojo.address.Address;
import com.doge.bean.pojo.address.AddressExample;
import com.doge.bean.pojo.region.Region;
import com.doge.bean.pojo.region.RegionExample;
import com.doge.bean.vo.address.WxAddressListVo;
import com.doge.exception.Param401Exception;
import com.doge.mapper.AddressMapper;
import com.doge.mapper.RegionMapper;
import com.doge.util.auth.GetSubject;
import com.doge.util.valid.ParamValidation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: doge
 * @description: 地址服务
 * @author: Mr.Jiang
 * @create: 2021-12-02 22:28
 **/
@Service
public class WxAddressServiceImpl implements WxAddressService{
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public WxAddressListVo list() {
        Integer userId = GetSubject.getUserId();
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        WxAddressListVo wxAddressListVo = new WxAddressListVo();
        wxAddressListVo.setLimit(addresses.size());
        wxAddressListVo.setList(addresses);
        wxAddressListVo.setPage(1);
        wxAddressListVo.setPages(1);
        wxAddressListVo.setTotal(addresses.size());
        return wxAddressListVo;
    }

    @Override
    public Address detail(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        return address;
    }
    @SneakyThrows
    @Override
    public Integer save(Address address) {
        if(address.getIsDefault()==true){
            updateAddressDefault();
        }
        boolean b = ParamValidation.phoneNumValId(address.getTel());
        if(!b){
            throw new Param401Exception("参数不对");
        }
        address.setAddTime(new Date());
        if(address.getId()==0){
            address.setUserId(GetSubject.getUserId());
            //获取地区编码
            RegionExample regionExample = new RegionExample();
            regionExample.createCriteria().andNameEqualTo(address.getCounty());
            List<Region> regions = regionMapper.selectByExample(regionExample);
            address.setAreaCode(regions.get(0).getCode().toString());
            address.setUpdateTime(new Date());
            addressMapper.insertSelective(address);
            return address.getId();
        }
        address.setUpdateTime(new Date());
        addressMapper.updateByPrimaryKeySelective(address);
        return address.getId();
    }

    @Override
    public void delete(Integer id) {
        Address address = new Address();
        address.setDeleted(true);
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andIdEqualTo(id);
        addressMapper.updateByExampleSelective(address,addressExample);
    }

    //修改所有默认为false
    private void updateAddressDefault(){
        Address address = new Address();
        address.setUpdateTime(new Date());
        address.setIsDefault(false);
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andDeletedEqualTo(false).andIsDefaultEqualTo(true);
        addressMapper.updateByExampleSelective(address,addressExample);
    }
}
